package com.ctsi.ssdc.admin.web;

import java.net.URI;
import java.net.URISyntaxException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import com.zdww.biyi.component.sdk.aop.BeanExposeMethodAble;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.data.domain.Pageable;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.ctsi.ssdc.admin.domain.dto.CscpRolesCriteria;
import com.ctsi.ssdc.admin.domain.dto.CscpRolesDTO;
import com.ctsi.ssdc.admin.service.CscpRolesService;
import com.ctsi.ssdc.exception.BadRequestAlertException;
import com.ctsi.ssdc.model.PageResult;
import com.ctsi.ssdc.util.HeaderUtil;

import static com.ctsi.ssdc.admin.consts.ComponentConstant.ADMIN;
import static com.ctsi.ssdc.admin.consts.ComponentConstant.METHOD;


/**
 * REST controller for managing CscpRoles.
 *
 * @author ctsi biyi generator
 *
 */
@RestController
@RequestMapping("/api/system")
public class CscpRolesController {

    private final Logger log = LoggerFactory.getLogger(CscpRolesController.class);

    private static final String ENTITY_NAME = "cscpRoles";

    private final CscpRolesService cscpRolesService;

    public CscpRolesController(CscpRolesService cscpRolesService) {
        this.cscpRolesService = cscpRolesService;
    }

    @InitBinder   
    public void initBinder(WebDataBinder binder) {   
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");   
        dateFormat.setLenient(true);   
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));   
    } 
    
    /**
     * POST  /cscpRoless : Create a new cscpRoles.
     *
     * @param cscpRolesDTO the cscpRolesDTO to create
     * @return the ResponseEntity with status 201 (Created) and with body the new cscpRolesDTO, or with status 400 (Bad Request) if the cscpRoles has already an ID
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @PostMapping("/cscpRoless")
    @BeanExposeMethodAble(component = ADMIN,method = METHOD)
    public ResponseEntity<CscpRolesDTO> createCscpRoles(@RequestBody CscpRolesDTO cscpRolesDTO) throws URISyntaxException {
        log.debug("REST request to save CscpRoles : {}", cscpRolesDTO);
        if (cscpRolesDTO.getId() != null) {
            throw new BadRequestAlertException("A new cscpRoles cannot already have an ID", ENTITY_NAME, "idexists");
        }
        CscpRolesDTO result = cscpRolesService.insert(cscpRolesDTO);
        return ResponseEntity.created(new URI("/api/cscpRoless/" + result.getId()))
            .headers(HeaderUtil.createEntityCreationAlert(ENTITY_NAME, result.getId().toString()))
            .body(result);
    }

    /**
     * PUT  /cscpRoless : Updates an existing cscpRoles.
     *
     * @param cscpRolesDTO the cscpRolesDTO to update
     * @return the ResponseEntity with status 200 (OK) and with body the updated cscpRolesDTO,
     * or with status 400 (Bad Request) if the cscpRolesDTO is not valid,
     * or with status 500 (Internal Server Error) if the cscpRolesDTO couldn't be updated
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @PutMapping("/cscpRoless")
    @BeanExposeMethodAble(component = ADMIN,method = METHOD)
    public ResponseEntity<CscpRolesDTO> updateCscpRoles(@RequestBody CscpRolesDTO cscpRolesDTO) throws URISyntaxException {
        log.debug("REST request to update CscpRoles : {}", cscpRolesDTO);
        if (cscpRolesDTO.getId() == null) {
            return createCscpRoles(cscpRolesDTO);
        }
        CscpRolesDTO result = cscpRolesService.update(cscpRolesDTO);
        return ResponseEntity.ok()
            .headers(HeaderUtil.createEntityUpdateAlert(ENTITY_NAME, cscpRolesDTO.getId().toString()))
            .body(result);
    }

    /**
     * GET  /cscpRoless : get the cscpRoless.
     *
     * @return the ResponseEntity with status 200 (OK) and the list of cscpRoless in body
     */
    @GetMapping("/cscpRoless")
    @BeanExposeMethodAble(component = ADMIN,method = METHOD)
    public PageResult<CscpRolesDTO> getCscpRoless(CscpRolesDTO cscpRolesDTO, Pageable page) {
        log.debug("REST request to get CscpRoless");
        return cscpRolesService.findByCscpRolesDTO(cscpRolesDTO, page);
    }
    
    /**
     * GET  /cscpRoless : get the cscpRoless.
     *
     * @return the ResponseEntity with status 200 (OK) and the list of cscpRoless in body
     */
    @GetMapping("/cscpRolessByCriteria")
    @BeanExposeMethodAble(component = ADMIN,method = METHOD)
    public PageResult<CscpRolesDTO> getCscpRolessByCriteria(CscpRolesCriteria cscpRolesCriteria, Pageable page) {
        log.debug("REST request to get CscpRolessByCriteria");
        return cscpRolesService.findByCscpRolesCriteria(cscpRolesCriteria, page);
    }

    /**
     * GET  /cscpRoless/:id : get the "id" cscpRoles.
     *
     * @param id the id of the cscpRolesDTO to retrieve
     * @return the ResponseEntity with status 200 (OK) and with body the cscpRolesDTO, or with status 404 (Not Found)
     */
    @GetMapping("/cscpRoless/{id}")
    @BeanExposeMethodAble(component = ADMIN,method = METHOD)
    public ResponseEntity<CscpRolesDTO> getCscpRoles(@PathVariable Integer id) {
        log.debug("REST request to get CscpRoles : {}", id);
        CscpRolesDTO cscpRolesDTO = cscpRolesService.findOne(id);
        return ResponseEntity.ok()
                .body(cscpRolesDTO);
        //return ResponseUtil.wrapOrNotFound(Optional.ofNullable(cscpRolesDTO));
    }

    /**
     * DELETE  /cscpRoless/:id : delete the "id" cscpRoles.
     *
     * @param id the id of the cscpRolesDTO to delete
     * @return the ResponseEntity with status 200 (OK)
     */
    @DeleteMapping("/cscpRoless/{id}")
    @BeanExposeMethodAble(component = ADMIN,method = METHOD)
    public ResponseEntity<Void> deleteCscpRoles(@PathVariable  Integer id) {
        log.debug("REST request to delete CscpRoles : {}", id);
        cscpRolesService.delete(id);
        return ResponseEntity.ok().headers(HeaderUtil.createEntityDeletionAlert(ENTITY_NAME, id.toString())).build();
    }
    		
}
